      subroutine plota_cont(p,fret,iter,numpar,numpc)

      INCLUDE 'param_dim.inc'
      save pz,xmin,xmax,ymin,ymax,iter_ant,ref_inic

      common/plota/ x,y,inc,N,NFIX,i_elet,nepe
      COMMON /cFDAT/ FILE_DAT,FILE_DEX,FILE_OPT,FILE_RES

! 	common/plotcav/NNINC,NINCL
	COMMON /incdata/ C0,CINC,NNINC,NUMPI,NUMPCI,NINCL,VARIA
      common /NPAR/ NPAR
      
      CHARACTER*11 FILE_B,FILE_BD
      CHARACTER*15 FILE_DAT,FILE_DEX,FILE_OPT,FILE_RES
      character*18 FILE_PLT,FILE_PLD,FILE_JPG,FILE_PNG,INICIAL,EXATO
      CHARACTER*16 FILE_PLDE,FILE_PLDF 
      character*14 FILE_P
      character*1 c_num1,barra,ip,pl,sc
      character*2 c_num2
      character*26 ref
      character*26 ref_inic
      character*7 xp,yp,xch,ych
      character*9 resid,resid_inic
      character*60 label
          
      integer NNINC(ninc_max),NINCL
	double precision CINC(ninc_max),C0
	integer NUMPI(ninc_max), NUMPCI(ninc_max)
	integer VARIA(ninc_max)
      INTEGER FPLT,FPLD,FPLDE,FPLDF

      DOUBLE PRECISION X(NX),Y(NX),compr,xc,yc,area,Ju,Jv,Juv
      double precision xcinc(ninc_max),ycinc(ninc_max)
      double precision areainc(ninc_max),comprinc(ninc_max)
      double precision Juinc(ninc_max),Jvinc(ninc_max),Juvinc(ninc_max)	

      INTEGER INC(NX,2),I_ELET(16,NELEX)
      double precision p(nparg_max), pz(npar_max)

   
      FPLT= 20
      FPLD= 21
      FPLDE= 22
      FPLDF= 23

      data iplota/-1/
      iplota=iplota+1

      barra= achar(92)
      pl= achar(39)
!       write(resid,'(f7.4)')fret
      write(resid,'(e9.2)')fret

      FILE_B= FILE_OPT

      if (iter.EQ.0)then
         iter_ant=-1
!          write(resid_inic,'(f7.4)')fret
         write(resid_inic,'(e9.2)')fret
         ref_inic='Inicial - Resid.='//resid_inic 
      endif

        
      if (iter.EQ.iter_ant)then ! iteracao simples ou composta (powell)
         sc=' '
      else
         sc=' '
      endif

      iter_ant=iter

      

      if (iter.LE.9) then
            write (c_num1,'(I1)') iter
            ref='Iter.0'//c_num1//' - Resid.='//resid
         else
            write (c_num2,'(I2)') iter
            ref='Iter.'//c_num2//' - Resid.='//resid
      endif
 
       if (iplota.LE.9) then
          write (c_num1,'(I1)') iplota
          FILE_PLT= FILE_B//'-0'//c_num1//'.plt'
       else
          write (c_num2,'(I2)') iplota
          FILE_PLT= FILE_B//'-'//c_num2//'.plt'
       endif
 

  
      FILE_P= FILE_PLT
      FILE_JPG= FILE_P//'.jpg'
      FILE_PLD= FILE_P//'.pld'
      FILE_PNG= FILE_P//'.png'

      FILE_BD=FILE_DEX
      EXATO=FILE_BD//'-00.pld'

      INICIAL=FILE_B//'-00.pld'

      FILE_PLDE= FILE_B//'.plde'
      FILE_PLDF= FILE_B//'.pldf'


      OPEN (FPLT,FILE=FILE_PLT,STATUS='UNKNOWN')

      OPEN (FPLD,FILE=FILE_PLD,STATUS='UNKNOWN')
      

!        NFIX=N-NPAR

       if (iter.EQ.0) then
		do i=1,NFIX
	
			if(i.eq.1) then
				xmin= x(i)
				xmax= x(i)
				ymin= y(i)
				ymax= y(i)
			else if(i.le.N) then
				if(xmin.gt.x(i)) xmin= x(i)
				if(ymin.gt.y(i)) ymin= y(i)
				if(xmax.lt.x(i)) xmax= x(i)
				if(ymax.lt.y(i)) ymax= y(i)
			end if
		
		end do

		dx = xmax - xmin
		dy = ymax - ymin
	
		if (dx.gt.dy)then
			ymin = ymin-(dx-dy)/2.
			ymax = ymax+(dx-dy)/2.
		else if (dy.gt.dx)then
			xmin = xmin-(dy-dx)/2.
			xmax = xmax+(dy-dx)/2.
		endif

		dx = xmax - xmin
		xmin = xmin - .15*dx
		xmax = xmax + .15*dx
		ymin = ymin - .15*dx
		ymax = ymax + .15*dx

              
          OPEN (FPLDE,FILE=FILE_PLDE,STATUS='UNKNOWN')
          OPEN (FPLDF,FILE=FILE_PLDF,STATUS='UNKNOWN')
          write(FPLDF,*)'# PARTE FIXA DO CONTORNO'
          do i=1,NFIX
             write(FPLDF,*) X(inc(i,1)),Y(inc(i,1))
             write(FPLDF,*) X(inc(i,2)),Y(inc(i,2))
          end do
       
          do ielet=1,16
             do ielem=1,nepe
                i= i_elet(ielet,ielem)
                write(FPLDE,*) X(inc(i,1)),Y(inc(i,1))
                write(FPLDE,*) X(inc(i,2)),Y(inc(i,2))
             enddo
             write(FPLDE,*)
          end do
          close (FPLDE)
          close (FPLDF)
       
       endif

      
       
       write (FPLD,*)'# PARTE MOVEL DO CONTORNO'
       do i=NFIX+1,N
           write(FPLD,*) X(inc(i,1)),Y(inc(i,1))
           write(FPLD,*) X(inc(i,2)),Y(inc(i,2))
  	   write(FPLD,*)
       end do



       write(FPLD,*)
       write (FPLD,*)'# Pontos de controle'
       
! 	write(*,*)'nincl',nincl
	ipc = 0
	do j=1,nincl
		do i=1,numpci(j)
			write(FPLD,*) p(3*ipc + 3*i-2),p(3*ipc + 3*i-1),p(3*ipc + 3*i)
		enddo
		write(FPLD,*)p(3*ipc + 1),p(3*ipc + 2),p(3*ipc + 3) 
		ipc = ipc + numpci(j)
		write(FPLD,*)
	enddo
       

	nfimant=NFIX
	do i=1,nincl
		nfimatual=nfimant+NNINC(i)
      		call propgeom(x,y,nfimatual,nfimant,compr,
     &				area,xc,yc,Ju,Jv,Juv)
!        		call centroide(x,y,nfimatual,nfimant,xc,yc,area)
		xcinc(i)=xc
		ycinc(i)=yc
		comprinc(i)=compr
		areainc(i)=abs(area)
		Juinc(i) = Ju
		Jvinc(i) = Jv
		Juvinc(i) = Juv

		nfimant=nfimatual
	enddo

        write(FPLD,*)'#   Centroide (x,y)   '

	do i=1,nincl
 		write (FPLD,*) xcinc(i),ycinc(i)
 		write (FPLD,*) 
	enddo
       write(xch,'(f7.3)') xc-.2
       write(ych,'(f7.3)') yc

       write(FPLD,*)
       write(FPLD,*)'#  xc, yc, area, perimetro, Ju, Jv, Juv:'
	do i=1,nincl
 		write (FPLD,1070) ' #  ',i,xcinc(i),ycinc(i),
     &		areainc(i),comprinc(i),Juinc(i),Jvinc(i),Juvinc(i)
	enddo
       close (FPLD)
       
       
        open(1,file='pgeom.tmp') !so para uma inclusao
	write (1,*) xcinc(1),ycinc(1),sqrt(xcinc(1)**2+ycinc(1)**2),
     &		areainc(1),comprinc(1),Juinc(1),Jvinc(1)
        close(1)

 1070  format(a4,i5,7f20.4)


       if (iter.EQ.0) then
          open(1,file='fobj0.tmp')
	  write(1,*)resid
	  close(1)     
          ref='Inicial - Resid.='//resid

c          label='set label '//pl//'G'//pl//' right front at '//
c     &                xch//','//ych//' tc lt 10'
c          write(FPLT,1040)label

          do i=1,numpc
             write(ip,'(I1)') i
             write(xp,'(f7.3)') p(3*i-2)+.2  
             write(yp,'(f7.3)') p(3*i-1)
             label= 'set label '//pl//ip//pl//
     &           ' left front at '//xp//','//yp//' tc lt 10'
             write(FPLT,1040)label
          enddo          
     
          write(FPLT,1050) FILE_B,
     &                  xmin,xmax,ymin,ymax,barra,
     &                  FILE_PLDF, barra, FILE_PLDE,barra,
     &                  EXATO,'Exato', barra,
     &                  FILE_PLD,ref,
     &                  './jpg/'//file_jpg,'./png/'//file_png


 1050     format('set key bottom', /,'set title',1h',a11,1h',/,
     &         'set size square',/,  
     &         'plot [',f10.2,':',f10.2,'][',f10.2,':',f10.2,']',a1,/,
     &            5x,1h',a16,1h',' notitle w linesp ,',a1,/,
     &            5x,1h',a16,1h',' notitle w linesp ,',a1,/,
     &  5x,1h',a18,1h',' t ',1h',a5,1h', ' w linesp lt 4 ps .4,',a1,/,
     &  5x,1h',a18,1h',' t ',1h',a26,1h',' w linesp lt 10 ps .4 ',/,
     &       'set terminal jpeg large size 600,600',/,
     &       'set output ',1h',a24,1h',/,'replot',/,
     &       '#set terminal png small size 400,400',/,
     &       '#set output ',1h',a24,1h',/,'replot')
       
       else
          
c          label='set label '//pl//'G'//pl//' right front at '//
c     &                   xch//','//ych//' tc lt 3'
c          write(FPLT,1040)label
	  open(1,file='fobjf.tmp')
	  write(1,*)resid
	  close(1)    
          do i=1,numpc
!              write(ip,'(I1)') i
!              write(xp,'(f7.3)') pz(3*i-2)+.2
!              write(yp,'(f7.3)') pz(3*i-1)
!              label= 'set label '//pl//ip//pl//
!      &              ' left front at '//xp//','//yp//' tc lt 10'
!              write(FPLT,1040)label
       
             write(ip,'(I1)') i
             write(xp,'(f7.3)') p(3*i-2)-.2
             write(yp,'(f7.3)') p(3*i-1)
             label= 'set label '//pl//ip//pl//
     &           ' right front at '//xp//','//yp//' tc lt 3'
             write(FPLT,1040)label
          enddo
 1040     format(a60)
    
          write(FPLT,1020) FILE_B,
     &                  xmin,xmax,ymin,ymax,barra,
     &                  FILE_PLDF, barra, FILE_PLDE,barra,
     &                  EXATO,'Exato', barra,
     &                  INICIAL,ref_inic, barra,
     &                  FILE_PLD,ref,
     &                  './jpg/'//file_jpg,'./png/'//file_png

 1020     format('set key bottom', /,'set title',1h',a11,1h',/,
     &         'set size square',/,  
     &         'plot [',f10.2,':',f10.2,'][',f10.2,':',f10.2,']',a1,/,
     &            5x,1h',a16,1h',' notitle w linesp ,',a1,/,
     &            5x,1h',a16,1h',' notitle w linesp ,',a1,/,
     &  5x,1h',a18,1h',' t ',1h',a5,1h', ' w linesp lt 4 ps .4,',a1,/,
     &  5x,1h',a18,1h',' t ',1h',a26,1h', ' w linesp lt 10 ps .4,',a1,/,
     &  5x,1h',a18,1h',' t ',1h',a26,1h',' w linesp lt 3 ps .4',/,
     &       'set terminal jpeg large size 600,600',/,
     &       'set output ',1h',a24,1h',/,'replot',/,
     &       '#set terminal png small size 400,400',/,
     &       '#set output ',1h',a24,1h',/,'replot')
       endif
     
       close(FPLT)

      call system('gnuplot '//FILE_PLT )


      return
      end

